/*******************************************************************************
 * Package: com.hadoop.act
 * Type:    Pv
 * Date:    2024-05-17 16:09
 *
 * Copyright (c) 2024 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hadoop.act;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

/**
 * 功能描述：统计访问量
 * 分析：统计访问量  count
 *
 * @author Songxianyang
 * @date 2024-05-17 16:09
 */
public class PvApp {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration configuration = new Configuration();

        Job job = Job.getInstance(configuration);
        // 设置启动类
        job.setJarByClass(PvApp.class);

        // 设置 map 、reduce 类
        job.setMapperClass(PvMapper.class);
        job.setReducerClass(PvReduce.class);

        // map 输出 类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //Reduce 输出类型
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(LongWritable.class);
        // 创建输入目录
        Path inputPath = new Path("D:\\work\\project\\gitee-github\\hadoop\\input\\act\\info.txt");
        // 创建输出目录
        Path outputPath = new Path("D:\\work\\project\\gitee-github\\hadoop\\output\\act\\");

        // 如果存在就删除
        File folder = new File(outputPath.toString());
        if (folder.exists()) {
            deleteFiles(folder);
        }

        // 设置 FileInputFormat   FileOutputFormat
        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job, outputPath);
        job.waitForCompletion(true);

    }


    private static void deleteFiles(File file) {
        if (file.isDirectory()) {
            // 如果是文件夹，则遍历其中的文件并递归删除
            for (File subFile : file.listFiles()) {
                deleteFiles(subFile);
            }
        }

        // 删除文件或空文件夹
        if (file.delete()) {
            System.out.println("已成功删除：" + file.getAbsolutePath());
        } else {
            System.out.println("删除失败：" + file.getAbsolutePath());
        }
    }

   static class PvMapper extends Mapper<LongWritable, Text, Text, LongWritable> {

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            Text keyText = new Text("key");
            LongWritable one = new LongWritable(1);
            context.write(keyText, one);
        }
    }

   static class PvReduce extends Reducer<Text, LongWritable, NullWritable, LongWritable> {

        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            long count = 0;
            for (LongWritable value : values) {
                count= count+value.get();
            }
            context.write(NullWritable.get(), new LongWritable(count));
        }
    }
}
